package edu.cmu.javadb.plan.condition;

import java.util.*;

import user.Node;

import edu.cmu.javadb.Attribute;
import edu.cmu.javadb.RecordInfo;
import edu.cmu.javadb.exceptions.NoAttributeException;

public abstract class Condition {

	/*
	 * problem 1: if we want to do like a.x > a.y problem 2:
	 */
	
	public static Path path() {
		return new Path();
	}
	
	public static Path path(String s) {
		return new Path(s);
	}
	
	public static Equal equal(Object left, Object right) {
		if (!(left instanceof Path)) {
			//
		}
		return new Equal((Path)left, right);
	}
	
	public static Greater greater(Object left, Object right) {
		if (!(left instanceof Path)) {
			//
		}
		return new Greater((Path)left, right);
	}
	
	public static Condition in(Object val, Object path) {
		return new In(val, path);
	}

	public static And and(Condition... conditions) {
		return new And(conditions);
	}

	public abstract List<Attribute> getAllFields(RecordInfo recInfo);

	public abstract boolean containRootPath();

	public abstract boolean containNonValBinary();

//	public abstract List<String> getAllFieldNames();

	public abstract boolean satisfy(Object o);

//	public static final char PATH = '$';

	public Object getPathValue(Object o, String[] path) {
		Object res = o;
		
		if (path.length == 0) {
			return o;
		}
		
		for (String s : path) {
			try {
				Attribute att = RecordInfo.getRecordInfo(res.getClass())
						.getAttribute(s);
				res = att.getValue(res);
			} catch (NoAttributeException e) {
				e.printStackTrace();
			}
		}
		return res;
	}

	public Object getValue(Object o, Object f) {
		if (f instanceof Path) {
			return getPathValue(o, ((Path) f).getPath());
		} else {
			return f;
		}
	}
}
